//
//  RBSettingViewController.m
//  Budejie
//
//  Created by 华杨科技 on 2017/9/23.
//  Copyright © 2017年 饶波. All rights reserved.
//

#import "RBSettingViewController.h"
#import "RBClearCacheCell.h"
#import "RBSettingCell.h"
#import "RBOtherCell.h"


@interface RBSettingViewController ()

@end

static NSString *RBClearCacheCellId = @"me_clearCached";
static NSString *RBSettingCellId = @"me_setting";


@implementation RBSettingViewController

- (instancetype)init{
    return [self initWithStyle:UITableViewStyleGrouped];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.title = @"设置";
    self.view.backgroundColor = RBCommonbgcolor; // 设置背景色，一般都在viewDidLoad方法中设置
     
//    NSLog(@"%lu", (unsigned long)[[SDImageCache sharedImageCache] getSize]);
//    [self getCacheSize];    
    
    // 注册cell
    [self.tableView registerClass:[RBClearCacheCell class] forCellReuseIdentifier:RBClearCacheCellId];
    [self.tableView registerClass:[RBSettingCell class] forCellReuseIdentifier:RBSettingCellId];
    [self.tableView registerNib:[UINib nibWithNibName:@"RBOtherCell" bundle:nil] forCellReuseIdentifier:@"me_other"];
    
}
- (void)getCacheSize 
{
    // 文件夹总大小
    NSUInteger size = 0;
    
    // 文件路径
    NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
    NSString *dirPath = [cachePath stringByAppendingPathComponent:@"default/com.hackemist.SDWebImageCache.default"];
     
    // 文件管理者
    NSFileManager *mgr = [NSFileManager defaultManager];

    // 迭代器
    NSDirectoryEnumerator *enumerator = [mgr enumeratorAtPath:dirPath];
    
    for (NSString *subpath in enumerator) {
        // 文件全路径
        NSString *fullPath = [dirPath stringByAppendingPathComponent:subpath];
        
        // 文件属性
        NSDictionary *attris = [mgr attributesOfItemAtPath:fullPath error:nil];
        
        // 累加文件和
        size += attris.fileSize;
    }
}

- (void)getCacheSize2 
{
    // 文件夹总大小
    NSUInteger size = 0;
    
    // 文件路径
    NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
    NSString *dirPath = [cachePath stringByAppendingPathComponent:@"MP3/Abc"];
    NSLog(@"%@",dirPath);
    
    // 文件管理者
    NSFileManager *mgr = [NSFileManager defaultManager];
    NSArray *subpathArr = [mgr subpathsAtPath:dirPath];
    
    for (NSString *subpath in subpathArr) {
        // 文件全路径
        NSString *fullPath = [dirPath stringByAppendingPathComponent:subpath];

        // 文件属性
        NSDictionary *attris = [mgr attributesOfItemAtPath:fullPath error:nil];
        
        // 累加文件和
        // size += [attris[NSFileSize] unsignedIntegerValue];
        size += attris.fileSize;
    }
    NSLog(@"\n size:%lu \n",size);    
}
 
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        if (indexPath.row == 0) {
            RBClearCacheCell *clearCell = [tableView dequeueReusableCellWithIdentifier:RBClearCacheCellId];
            return clearCell;
        }else {
            
            RBSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:RBSettingCellId];
            if (indexPath.row == 1) {
                cell.textLabel.text = @"推荐关注";
            } else if (indexPath.row){
                cell.textLabel.text = @"给我们好评";
            }else {
                cell.textLabel.text = @"其他cell";
            }
            return cell;
        }
    }else {
        RBOtherCell *cell = [tableView dequeueReusableCellWithIdentifier:@"me_other"];
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    RBNSLogFunction;
}

- (void)dealloc {
    RBNSLogFunction;
}

@end
